class HotelsController < ApplicationController
  # GET /hotels
  # GET /hotels.json

  layout 'admin'

  helper_method :sort_column, :sort_direction, :hotel_search

  def index
    @hotels = Hotel.order(sort_column + ' ' + sort_direction)
                         .paginate(:per_page => 10, :page => params[:page])

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  # GET /hotels/1
  # GET /hotels/1.json
  def show
    @hotel = Hotel.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @hotel }
    end
  end

  # GET /hotels/new
  # GET /hotels/new.json
  def new
    @hotel = Hotel.new

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  # GET /hotels/1/edit
  def edit
    @hotel = Hotel.find(params[:id])

    respond_to do |format|   
      format.js { render patial: false }    
    end
  end

  # POST /hotels
  # POST /hotels.json
  def create
    @hotel = Hotel.new(params[:hotel])
    @hotel.userid = session[:user_id].to_i
    @hotel.save
    @hotels = Hotel.order(sort_column + ' ' + sort_direction)
                         .paginate(:per_page => 10, :page => params[:page])
    
    respond_to do |format|
      format.js
    end
  end

  # PUT /hotels/1
  # PUT /hotels/1.json
  def update
    @hotel = Hotel.find(params[:id])
    @hotel.update_attributes(params[:hotel])
    @hotels = Hotel.order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])

    respond_to do |format|   
      format.js { render layout: false }   
    end
  end

  # DELETE /hotels/1
  # DELETE /hotels/1.json
  def destroy
    @hotel = Hotel.find(params[:id])
    @hotel.destroy
    @hotels = Hotel.order(sort_column + ' ' + sort_direction)
                         .paginate(:per_page => 10, :page => params[:page])

    respond_to do |format|   
      format.js { render layout: false }   
    end
  end

  def destroy_multiple
    Hotel.destroy(params[:hotels_ids])
    @hotels = Hotel.order(sort_column + ' ' + sort_direction)
                         .paginate(:per_page => 10, :page => params[:page])

    respond_to do |format|
      flash[:notice] = "Selected hotel was successfully deleted."
      format.js { render layout: false }
      format.json { head :no_content }
    end
  end

  def facility_list
    @facilities = Facility.all
    @hotel_id = params[:hotel_id]

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  def facility_selected
    if params[:hid] and params[:hotels_facilities]
      HotelFacility.where(:hotel_id => params[:hid]).destroy_all
      params[:hotels_facilities].each do |a|
        hotel_fac = HotelFacility.new
        hotel_fac.hotel_id = params[:hid]
        hotel_fac.facility_id = a
        @saved = true  if hotel_fac.save
        @hotels = Hotel.order(sort_column + ' ' + sort_direction)
                         .paginate(:per_page => 10, :page => params[:page])
      end
    end
    respond_to do |format|
      format.js { render layout: false }
    end
  end

  private
    def sort_direction
      %w[asc desc].include?(params[:direction]) ?  params[:direction] : "asc"
    end

    def sort_column
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'created_at' ? params[:sort] : "created_at"
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'updated_at' ? params[:sort] : "updated_at"
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'is_published' ? params[:sort] : "is_published"
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'is_on_promoted' ? params[:sort] : "is_on_promoted"
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'is_recomended' ? params[:sort] : "is_recomended"
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'area_id' ? params[:sort] : "province_id"
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'hotel_name' ? params[:sort] : "hotel_name"
    end

   def hotel_search
    if params[:search]
      session[:hotel_search] = params[:search]
    elsif params[:clear_session]
      session[:hotel_search] = nil
    end
    results = session[:hotel_search]
    results
  end
end
